<template>
  <div>
    <ToolBar>
      <el-button type="primary" icon="el-icon-plus" size="small" @click="addContent()">添加</el-button>
      <div style="float: right">
        <el-input
                placeholder="搜索"
                size="small"
                style="width: 140px"
                v-model="searchParams.name"
                clearable>
        </el-input>
        <el-button type="success" icon="el-icon-search" size="small" @click="getList()"></el-button>
      </div>
    </ToolBar>
    <el-table
            :data="tableData"
            border
            style="width: 100%">
      <el-table-column
              prop="id"
              label="id"
              width="180">
      </el-table-column>
      <el-table-column
              prop="name"
              label="名称"
              width="180">
      </el-table-column>
      <el-table-column
              prop="courseId"
              label="课程id">
      </el-table-column>
      <el-table-column
              label="操作"
              :render-header="tableAction"
              width="120">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="info" icon="el-icon-info" size="small" circle></el-button>
          <el-button @click="handleClick(scope.row)" type="primary" icon="el-icon-edit" size="small" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
            style="display: inline-block"
            background
            layout="prev, pager, next"
            :current-page.sync="searchParams.page"
            :page-size="searchParams.size"
            :total="page.total"
            @current-change="getList()">
    </el-pagination>
    <ContentEdit
            :title="title"
            :dialogFormVisible="visible"
            :data="data"
            @val-change="addChapter"
            @cancel="visible = false"/>

  </div>
</template>

<script>
    import business from './../../api/business';
    import ToolBar from '~/components/ToolBar/ToolBar.vue';
    import HelpHint from '~/components/HelpHint/HelpHint.vue';
    import ContentEdit from '~/views/ContentManage/ContentEdit.vue';

    const {chapter, chapterAdd} = business
    export default {
        data() {
            return {
                title: '角色编辑',
                data: false,
                visible: false,
                page: {
                    total: 0
                },
                searchParams: {
                    page: 1,
                    size: 5,
                    name: 'published',
                },
                tableData: []
            }
        },
        created: function () {
            this.getList()
        },
        methods: {
            handleClick(row) {
                this.$alert(row, '标题名称', {
                    confirmButtonText: '确定',
                    callback: action => {
                    }
                });
            },
            tableAction() {
                return this.$createElement('HelpHint', {
                    props: {
                        content: '查看文章 / 编辑文章'
                    }
                }, '操作');
            },
            getList() {
                chapter(this.searchParams).then((response) => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                })
            },
            addContent() {
                this.visible = true
            },
            addChapter(row) {
                chapterAdd(row).then(() => {
                    console.log("唱")
                })
            },
            cancel(){
                this.dialogFormVisible = false
            }
        },
        components: {
            ContentEdit,
            ToolBar, HelpHint
        }
    }
</script>